home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / GUSI / include / GUSIINET_P.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-31  |  4.9 KB  |  159 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    GUSI                -    Grand Unified Socket Interface
  3. File        :    GUSIINET_P.h    -    Common definitions for TCP/IP Sockets
  4. Author    :    Matthias Neeracher
  5.  
  6.     This file was derived from the socket library by 
  7.     
  8.         Charlie Reiman    <creiman@ncsa.uiuc.edu> and
  9.         Tom Milligan    <milligan@madhaus.utcs.utoronto.ca>
  10.           
  11. Started    :    16Aug92                                Language    :    MPW C/C++
  12. Modified    :    20Aug92    MN    First approach
  13.                 23Aug92    MN    Available()
  14.                 08Jan93    MN    tcp_notify was setting the wrong stream to unconnected
  15.                 21Jan93    MN    Remove torecv
  16.                 31Jan93    MN    Support for inetd
  17.                 17Jun93    MN    UDPSocket::getsockname()
  18.                 01Sep93    MN    Turn terminus into a short
  19. Last        :    01Sep93
  20. *********************************************************************/
  21.  
  22. #include "GUSI_P.h"
  23.  
  24. #include <Devices.h>
  25. #include <MacTCPCommonTypes.h>
  26. #include <TCPPB.h>
  27. #include <UDPPB.h>
  28. #include <MiscIPPB.h>
  29. #include <AddressXlation.h>
  30. #include <GetMyIPAddr.h>
  31.  
  32. #define STREAM_BUFFER_SIZE    8192
  33. #define UDP_MAX_MSG            65507    /* MacTCP max legal udp message */
  34. #define TCP_MAX_MSG            65535    /* MacTCP max legal tcp message */
  35.  
  36. #define TCP_MAX_WDS        4        /* arbitrary number of wds to alloc in sock_tcp_send */
  37.  
  38. class INETSocket : public Socket    {                // That's what this file's all about
  39.     friend class INETSocketDomain;    
  40.  
  41. protected:
  42.     StreamPtr                stream;        /* stream pointer */
  43.     byte                        status;        /* Is file descriptor in use */
  44.     Boolean                    nonblocking;/* socket set for non-blocking I/O. */
  45.     char *                    recvBuf;        /* receive buffer */
  46.     int                        recvd;        /* amount received */
  47.     struct sockaddr_in    sa;            /* My address. */
  48.     struct sockaddr_in    peer;            /* Her address. */
  49.     byte                        sstate;        /* socket's connection state. */
  50.     int                        asyncerr;    /* Last async error to arrive.  zero if none. */
  51.     
  52.                     INETSocket();
  53.                     INETSocket(StreamPtr stream);
  54.                     
  55.     virtual         ~INETSocket();
  56.     
  57.     virtual u_long    Available();
  58. public:
  59.     virtual int        bind(void * name, int namelen);
  60.     virtual int     getsockname(void * name, int * namelen);
  61.     virtual int     getpeername(void * name, int * namelen);
  62.     virtual int        fcntl(unsigned int cmd, int arg);
  63.     virtual int        ioctl(unsigned int request, void *argp);
  64.     virtual int     shutdown(int how);
  65. };    
  66.  
  67. class AnnotatedPB;
  68.  
  69. class TCPSocket : public INETSocket    {    
  70.     friend class INETSocketDomain;
  71.     friend pascal void tcp_notify(StreamPtr, u_short, Ptr, u_short, struct ICMPReport *);
  72.     friend void tcp_connect_done(AnnotatedPB *);
  73.     friend void tcp_listen_done(AnnotatedPB *);
  74.     friend void tcp_recv_done(AnnotatedPB *);
  75.     friend void tcp_send_done(AnnotatedPB *);
  76.     friend pascal OSErr TCPPossession(AppleEvent*, AppleEvent*, long);
  77.  
  78. // Since an userDataPtr may not be changed during the lifetime of a stream, we need
  79. // this indirection. The class invariant *(this->self) == this holds.
  80.  
  81.     TCPSocket **    self;
  82.  
  83.                         TCPSocket();
  84.                         TCPSocket(StreamPtr stream);
  85.                         TCPSocket(TCPSocket * sock);
  86.                     
  87.     virtual             ~TCPSocket();
  88.     TCPiopb *        GetPB();
  89.     virtual u_long    Available();
  90. public:
  91.     virtual int     connect(void * address, int addrlen);
  92.     virtual int     listen(int qlen);
  93.     virtual Socket * accept(void * address, int * addrlen);
  94.     virtual int     recvfrom(void * buffer, int buflen, int flags, void * from, int * fromlen);
  95.     virtual int     sendto(void * buffer, int buflen, int flags, void * to, int tolen);
  96.     virtual int     select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
  97. };    
  98.  
  99. class UDPSocket : public INETSocket    {    
  100.     friend class INETSocketDomain;    
  101.     friend void udp_read_ahead_done(AnnotatedPB *);
  102.     friend pascal OSErr UDPPossession(AppleEvent*, AppleEvent*, long);
  103.  
  104.                         UDPSocket();
  105.                         UDPSocket(StreamPtr stream);
  106.     virtual             ~UDPSocket();
  107.     UDPiopb *        GetPB();
  108.     virtual u_long    Available();
  109.     int                NewStream();
  110.     int                FlushReadAhead();
  111.     int                ReadAhead();
  112. public:
  113.     virtual int     getsockname(void * name, int * namelen);
  114.     virtual int     connect(void * address, int addrlen);
  115.     virtual int     recvfrom(void * buffer, int buflen, int flags, void * from, int * fromlen);
  116.     virtual int     sendto(void * buffer, int buflen, int flags, void * to, int tolen);
  117.     virtual int     select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
  118. };    
  119.  
  120. class AnnotatedPB {
  121.     union {
  122.         TCPiopb        tcp;
  123.         UDPiopb        udp;
  124.     } pb;
  125.     INETSocket    *    sock;
  126. public:
  127.     void                SetOwner(INETSocket * s)        {    sock    =    s;                            }
  128.     INETSocket *    Owner()                                {    return sock;                        }
  129.     TCPiopb *        TCP()                                    {    return &pb.tcp;                    }
  130.     UDPiopb *        UDP()                                    {    return &pb.udp;                    }
  131.     Boolean            Busy()                                {    return pb.tcp.ioResult == 1;    }
  132. };
  133.  
  134. struct miniwds {
  135.     u_short    length;
  136.     char *     ptr;
  137.     short        terminus;    /* must be zero'd for use */
  138. };
  139.  
  140. class INETSocketDomain : public SocketDomain {
  141.     OSErr                driverState;
  142.     OSErr                resolverState;
  143.     short                drvrRefNum;
  144.     short             pbLast;                                /* last pb used */
  145.     AnnotatedPB *    pbList;                                /* The pb array */
  146. public:
  147.     INETSocketDomain();
  148.     
  149.     short                Driver();
  150.     OSErr                Resolver();
  151.     AnnotatedPB *    GetPB();
  152.     
  153.     virtual Socket *     socket(int type, short protocol);
  154. };
  155.  
  156. extern INETSocketDomain    INETSockets;
  157.  
  158. int TCP_error(int MacTCPerr);
  159.